﻿Namespace Saglik.Ilac
	Module Functions

		Sub IlacKaydet(ByVal Process As Token, ByVal Etken As String, ByVal Ad As String, ByVal Barkod As String, ByVal Fiyat As Decimal)
			Etken = Mid(Etken, 1, 64)
			Dim Rs As Recordset = Process.OpenSQL("select rowid from ilacetken where adi = :~adi", Etken)
			If Rs.EOF Then
				Process.ExecSQL("insert into ilacetken (adi) values (:~adi)", Etken)
				Rs = Process.OpenSQL("select max(rowid) from ilacetken")
			End If
			Dim Kod As Integer = Rs(0)
			Rs = Process.OpenSQL("select * from ilac where adi = :~adi", Ad)
			If Rs.EOF Then Rs.AddNew()
			Rs.SetValues("adi,barkod,etken,fiyat,islemtar", Ad, Barkod, Kod, Fiyat, Process.SysDate)
			Rs.Save()
		End Sub

		Function Kaydet(ByVal Token As Token, ilactkod As String, ilactadi As String, ilacfkod As String, ilacfadi As String, ist1 As String, patent As String, barkodlar As List(Of String), kaykul As String) As Result
			Try
				Token.BeginTran()
				Dim islemtar = Token.SysTime
				Dim Rsi = Token.OpenSQL("select * from ssdmerkez.dbo.ilactisim where ilactkod = @ilactkod", ilactkod)
				If Rsi.EOF Then Rsi.AddNew()
				Rsi.SetValues("ilacfkod,ilactkod,ilactadi,patent,kaykul,islemtar", ilacfkod, ilactkod, ilactadi, patent, kaykul, islemtar)
				Rsi.Save()
				Dim Rsf = Token.OpenSQL("select * from ssdmerkez.dbo.ilacfrml where ilacfkod = @ilacfkod", ilacfkod)
				If Rsf.EOF Then Rsf.AddNew()
				Rsf.SetValues("ilacfkod,ilacfadi,ist1,kaykul,islemtar,ilacfturu", ilacfkod, ilacfadi, ist1, kaykul, islemtar, "")
				Rsf.Save()
				Token.ExecSQL("delete from ssdmerkez.dbo.ilacbarkod where ilactkod = @ilactkod", ilactkod)
				Dim Rsb = Token.OpenSQL("select * from ssdmerkez.dbo.ilacbarkod where ilactkod = @ilactkod", ilactkod)
				For Each barkod In barkodlar
					Rsb.AddNew("ilactkod,barkod,kaykul,islemtar", ilactkod, barkod, kaykul, islemtar)
				Next
				Rsb.Save()
				Token.CommitTran()
				Return Result.OK("")
			Catch ex As Exception
				Return Result.Error(ex.Message)
			End Try
		End Function

		Function Oku(Token As Token, ilactkod As String) As Recordset
			Q("select a.*,b.ilacfadi,b.ist1 from ssdmerkez..ilactisim a inner join ssdmerkez..ilacfrml b on a.ilacfkod = b.ilacfkod where a.ilactkod = @ilactkod")
			Return Token.OpenSQL("*", ilactkod)
		End Function

		Function OkuFormul(Token As Token, ilacfkod As String) As Recordset
			Q("select a.*,b.ilacfadi,b.ist1 from ssdmerkez..ilactisim a inner join ssdmerkez..ilacfrml b on a.ilacfkod = b.ilacfkod where a.ilacfkod = @ilacfkod")
			Return Token.OpenSQL("*", ilacfkod)
		End Function

		Function OkuFiyat(Token As Token, ilactkod As String) As Decimal
			Return Token.OpenSQL("select top 1 fiat from ssdmerkez..ilacfiat where ilactkod = @ilactkod order by tarih desc", ilactkod)(0)
		End Function

		Function BarkodOku(Token As Token, ilactkod As String) As Recordset
			Q("select * from ssdmerkez..ilacbarkod where ilactkod = @ilactkod")
			Return Token.OpenSQL("*", ilactkod)
		End Function

		Function BarkodIlac(Token As Token, barkod As String) As Recordset
			Q("select a.ilactkod,ilacfadi,")
			Q("(select top 1 fiat from ssdmerkez..ilacfiat where ilactkod = a.ilactkod order by tarih desc) as fiyat")
			Q("from ssdmerkez..ilacbarkod a inner join ssdmerkez..ilactisim b on a.ilactkod = b.ilactkod")
			Q("inner join ssdmerkez..ilacfrml c on b.ilacfkod = c.ilacfkod")
			Q("where barkod = @ilactkod")
			Return Token.OpenSQL("*", barkod)
		End Function

		Function Liste(Token As Token) As Recordset
			Q("select a.ilactkod,a.ilactadi,b.ilacfkod,b.ilacfadi,")
			Q("(select top 1 fiat from ssdmerkez..ilacfiat where ilactkod = a.ilactkod order by islemtar desc) as fiyat")
			Q("from ssdmerkez..ilactisim a left join ssdmerkez..ilacfrml b on a.ilacfkod = b.ilacfkod")
			Q("order by ilactadi")
			Return Token.OpenSQL("*")
		End Function

		Function GucListe(Token As Token) As Recordset
			Q("select * from ssdmerkez..gucliste")
			Return Token.OpenSQL("*")
		End Function

		Function GucListeMuadil(Token As Token) As Recordset
			Q("select b.barkod as barkod1,b.adi as ilac1,c.adi as ilac2,c.barkod as barkod2,b.jenerik")
			Q("from ssdmerkez..ilacbarkod a inner join ssdmerkez..gucliste b on a.barkod = b.barkod")
			Q("inner join ssdmerkez..gucliste c on b.jenerik = c.jenerik")
			Q("where b.barkod <> c.barkod")
			Q("order by b.adi")
			Return Token.OpenSQL("*")
		End Function

		Sub BarkodKaydet(Token As Token, ilactkod As String, barkod As String, kaykul As String)
			Dim Rs = Token.OpenSQL("select * from ssdmerkez.dbo.ilacbarkod where ilactkod = @ilactkod", ilactkod)
			Rs.AddNew("ilactkod,barkod,kaykul,islemtar", ilactkod, barkod, kaykul, Token.SysDate)
			Rs.Save()
		End Sub

		Sub MuadilKaydet(Token As Token, List As List(Of String))
			Token.BeginTran()
			For Each Item In List
				Dim Items = Item.Split(".")
				Q("insert into ssdmerkez..ilacbarkod (ilactkod,barkod,kaykul,islemtar)")
				Q("select ilactkod,@muadil,'0000',getdate() from ssdmerkez..ilacbarkod where barkod = @barkod")
				Token.ExecSQL("*", Items(1), Items(0))
			Next
			Token.CommitTran()
		End Sub

	End Module
End Namespace
